3 use MediaWiki\MediaWikiServices
;
6 * Wraps the user object, so we can also retain full access to properties
7 * like password if we log in via the API.
25 private function assertNotReal() {
27 if ( $wgDBprefix !== MediaWikiTestCase
::DB_PREFIX
&&
28 $wgDBprefix !== MediaWikiTestCase
::ORA_DB_PREFIX
30 throw new MWException( "Can't create user on real database" );
34 public function __construct( $username, $realname = 'Real Name',
35 $email = 'sample@example.com', $groups = []
37 $this->assertNotReal();
39 $this->username
= $username;
40 $this->password
= 'TestUser';
42 $this->user
= User
::newFromName( $this->username
);
45 // In an ideal world we'd have a new wiki (or mock data store) for every single test.
46 // But for now, we just need to create or update the user with the desired properties.
47 // we particularly need the new password, since we just generated it randomly.
48 // In core MediaWiki, there is no functionality to delete users, so this is the best we can do.
49 if ( !$this->user
->isLoggedIn() ) {
51 $this->user
= User
::createNew(
54 "real_name" => $realname
59 throw new MWException( "Error creating TestUser " . $username );
63 // Update the user to use the password and other details
64 $this->setPassword( $this->password
);
65 $change = $this->setEmail( $email ) ||
66 $this->setRealName( $realname );
68 // Adjust groups by adding any missing ones and removing any extras
69 $currentGroups = $this->user
->getGroups();
70 foreach ( array_diff( $groups, $currentGroups ) as $group ) {
71 $this->user
->addGroup( $group );
73 foreach ( array_diff( $currentGroups, $groups ) as $group ) {
74 $this->user
->removeGroup( $group );
77 // Disable CAS check before saving. The User object may have been initialized from cached
78 // information that may be out of whack with the database during testing. If tests were
79 // perfectly isolated, this would not happen. But if it does happen, let's just ignore the
80 // inconsistency, and just write the data we want - during testing, we are not worried
82 $this->user
->mTouched
= '';
83 $this->user
->saveSettings();
88 * @param string $realname
91 private function setRealName( $realname ) {
92 if ( $this->user
->getRealName() !== $realname ) {
93 $this->user
->setRealName( $realname );
101 * @param string $email
104 private function setEmail( $email ) {
105 if ( $this->user
->getEmail() !== $email ) {
106 $this->user
->setEmail( $email );
114 * @param string $password
116 private function setPassword( $password ) {
117 self
::setPasswordForUser( $this->user
, $password );
121 * Set the password on a testing user
123 * This assumes we're still using the generic AuthManager config from
124 * PHPUnitMaintClass::finalSetup(), and just sets the password in the
127 * @param string $password
129 public static function setPasswordForUser( User
$user, $password ) {
130 if ( !$user->getId() ) {
131 throw new MWException( "Passed User has not been added to the database yet!" );
134 $dbw = wfGetDB( DB_MASTER
);
135 $row = $dbw->selectRow(
138 [ 'user_id' => $user->getId() ],
142 throw new MWException( "Passed User has an ID but is not in the database?" );
145 $passwordFactory = MediaWikiServices
::getInstance()->getPasswordFactory();
146 if ( !$passwordFactory->newFromCiphertext( $row->user_password
)->equals( $password ) ) {
147 $passwordHash = $passwordFactory->newFromPlaintext( $password );
150 [ 'user_password' => $passwordHash->toString() ],
151 [ 'user_id' => $user->getId() ],
161 public function getUser() {
169 public function getPassword() {
170 return $this->password
;